package com.example.social.screen

import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Add
import androidx.compose.material.icons.rounded.Home
import androidx.compose.material.icons.rounded.Notifications
import androidx.compose.material.icons.rounded.Person
import androidx.compose.material.icons.rounded.Search
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.example.social.model.BottomNavItem
import com.example.social.navigation.Routes

@Composable
fun BottomNav(navController: NavHostController) {
    val navController1 = rememberNavController()
    Scaffold(bottomBar = { MyBottomBar(navController1) }) { innerPadding ->
        NavHost(
            navController = navController1,
            startDestination = Routes.Home.routes,
            modifier = Modifier.padding(innerPadding)
        ) {
            composable(route = Routes.Home.routes) {
                Home(navController1,navController)
            }
            composable(route = Routes.Notification.routes) {
                Notification(navController1,navController)
            }
            composable(route = Routes.Search.routes) {
                Search(navController1,navController)
            }
            composable(route = Routes.AddThreads.routes) {
                AddThreads(navController1)
            }
            composable(route = Routes.Profile.routes) {
                Profile(navController1,navController)
            }
        }
    }
}


@Composable
fun MyBottomBar(navController1: NavHostController) {
    val backStackEntry = navController1.currentBackStackEntryAsState()
    val list = listOf(
        BottomNavItem(
            title = "主页",
            route = Routes.Home.routes,
            icon = Icons.Rounded.Home
        ),
        BottomNavItem(
            title = "搜索",
            route = Routes.Search.routes,
            icon = Icons.Rounded.Search
        ),
        BottomNavItem(
            title = "添加话题",
            route = Routes.AddThreads.routes,
            icon = Icons.Rounded.Add
        ), BottomNavItem(
            title = "消息",
            route = Routes.Notification.routes,
            icon = Icons.Rounded.Notifications
        ), BottomNavItem(
            title = "个人",
            route = Routes.Profile.routes,
            icon = Icons.Rounded.Person
        )
    )

    BottomAppBar {
        list.forEach {
            val selected = it.route == backStackEntry.value?.destination?.route
            NavigationBarItem(
                selected = selected,
                onClick = {
                    navController1.navigate(it.route) {
                        popUpTo(navController1.graph.findStartDestination().id) {
                            saveState = true
                            //回到首页时保留之前的导航历史，避免重新加载
                        }
                        launchSingleTop = true
                    }
                },
                icon = { Icon(imageVector = it.icon, contentDescription = it.title) })
        }
    }
}
